package cn.edu.cug.cs.gtl.utils.impl;

import cn.edu.cug.cs.gtl.protos.DataCloud;
import cn.edu.cug.cs.gtl.protos.StorageCluster;
import cn.edu.cug.cs.gtl.utils.DataDeviceManager;
import cn.edu.cug.cs.gtl.utils.DatabaseUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DataDeviceManagerImpl implements DataDeviceManager {
    private Connection con;

    public DataDeviceManagerImpl() throws SQLException {
        con=DatabaseUtil.getConnection();
    }

    @Override
    public void insertDataCloud(DataCloud dataCloud) {

    }

    @Override
    public void insertStorageCluster(StorageCluster storageCluster) {
        String insertSQL="insert into storage_cluster(name, alias, description, type, url, user_name, password, dc_id) VALUES (?,?,?,?,?,?,?,?)";

        try {
            PreparedStatement preparedStatement = con.prepareStatement(insertSQL);
            preparedStatement.setString(1, storageCluster.getName());
            preparedStatement.setString(2, storageCluster.getAlias());
            preparedStatement.setString(3, storageCluster.getDescription());
            preparedStatement.setString(4, storageCluster.getType());
            preparedStatement.setString(5, storageCluster.getUrl());
            preparedStatement.setString(6, storageCluster.getUserName());
            preparedStatement.setString(7, storageCluster.getPassword());
            preparedStatement.setInt(8,1);
            //preparedStatement.setString(8, storageCluster.getDcId());

            int rowsAffected = preparedStatement.executeUpdate();

            //检查是否成功插入一条记录
            if(rowsAffected>0){
                System.out.println(storageCluster.getName()+"插入成功");
            }
            else{
                System.err.println("插入失败");
            }

            preparedStatement.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateStorageCluster(StorageCluster storageCluster,int id) {
        String updateSQL="update storage_cluster set name=?,alias=?,description=?,type=?,url=?,user_name=?,password=?,dc_id=? where id=?";

        try {
            PreparedStatement preparedStatement = con.prepareStatement(updateSQL);
            preparedStatement.setString(1, storageCluster.getName());
            preparedStatement.setString(2, storageCluster.getAlias());
            preparedStatement.setString(3, storageCluster.getDescription());
            preparedStatement.setString(4, storageCluster.getType());
            preparedStatement.setString(5, storageCluster.getUrl());
            preparedStatement.setString(6, storageCluster.getUserName());
            preparedStatement.setString(7, storageCluster.getPassword());
            preparedStatement.setInt(8,1);
            //preparedStatement.setString(8, storageCluster.getDcId());

            preparedStatement.setInt(9,id);

            int rowsAffected = preparedStatement.executeUpdate();

            //检查是否成功插入一条记录
            if(rowsAffected>0){
                System.out.println(storageCluster.getName()+"插入成功");
            }
            else{
                System.err.println("插入失败");
            }

            preparedStatement.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public StorageCluster removeStorageCluster(String storageClusterName) {
        String deleteSQL="delete from storage_cluster where name=?";


        return null;
    }

    @Override
    public DataCloud getDataCloud() {
        return null;
    }
}
